/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bground.game;

import bground.game.GL.Direction;
import bground.collisions.BulletFieldCollision;
import bground.collisions.PlayerBulletCollision;
import bground.collisions.PlayerFieldCollision;
import bground.sprites.Border;
import bground.sprites.Enemy;
import bground.sprites.Player;
import bground.sprites.Tree;
import external.golden.gamedev.Game;
import external.golden.gamedev.GameLoader;
import external.golden.gamedev.object.Background;
import external.golden.gamedev.object.PlayField;
import external.golden.gamedev.object.Sprite;
import external.golden.gamedev.object.SpriteGroup;
import external.golden.gamedev.object.background.ColorBackground;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;

/**
 *
 * @author s6_kustan_e
 */
public class BGround extends Game implements GameField {

    {
        distribute = true;
    }

    PlayField playfield;
    Background background;
    SpriteGroup players;
    SpriteGroup bullets;
    SpriteGroup field;
    Player me;
    Player enemy;
    public void addPlayer(Sprite s){
        players.add(s);
    }
    public void addBullet(Sprite s){
        bullets.add(s);
    }
    public void addField(Sprite s){
        field.add(s);
    }

    @Override
    public void initResources() {
        playfield = new PlayField();
        background = new ColorBackground(Color.YELLOW, 5000, 5000);
        
        players = new SpriteGroup("Players");
        bullets = new SpriteGroup("Bullets");
        field = new SpriteGroup("Field");

        playfield.setBackground(background);
        me = new Player(this);
        me.setLocation(1000, 750);
        me.setBackground(background);
        addPlayer(me);

        enemy = new Player(this);
        enemy.setLocation(100, 100);
        enemy.setBackground(background);
        addPlayer(enemy);

        enemy = new Player(this);
        enemy.setLocation(1000, 100);
        enemy.setBackground(background);
        addPlayer(enemy);
        Enemy e;

        for (int i = 0; i < 10; i++) {
            e = new Enemy(this);
            enemy.setBackground(background);
            addPlayer(e);
        }
        for (int i = 250; i < 1000; i += 100) {
            for (int j = 250; j < 1000; j += 100) {
                Tree tree = new Tree();
                tree.setLocation(i, j);
                tree.setBackground(background);
                addField(tree);
            }

        }

        for (int i = 0; i <= 4950; i += 50) {
            for (int j = 0; j <= 4950; j += 50) {
                if ((i % 4950) * (j % 4950) == 0) {
                    Border b = new Border();
                    b.setLocation(i, j);
                    b.setBackground(background);
                    addField(b);
                }
            }

        }

        initNet();
        background.setToCenter(me);
        playfield.addGroup(players);
        playfield.addGroup(bullets);
        playfield.addGroup(field);

        playfield.addCollisionGroup(players, bullets, new PlayerBulletCollision());
        playfield.addCollisionGroup(players, field, new PlayerFieldCollision());
        playfield.addCollisionGroup(bullets, field, new BulletFieldCollision());



        setFPS(60);
    }

    @Override
    public void update(long l) {

        if (keyDown(KeyEvent.VK_W)) {
//            me.setSpeed(0, -0.2);
            me.move(Direction.up);
        }
        if (keyDown(KeyEvent.VK_A)) {
//            me.setSpeed(-0.2, 0);
            me.move(Direction.left);
        }
        if (keyDown(KeyEvent.VK_S)) {
//            me.setSpeed(0, 0.2);
            me.move(Direction.down);
        }
        if (keyDown(KeyEvent.VK_D)) {
//            me.setSpeed(0.2, 0);
            me.move(Direction.right);

        }
        if (keyPressed(KeyEvent.VK_SPACE)) {
            me.fire();
        }
        background.setToCenter(me);


        players.remove(players.getInactiveSprite());
        playfield.update(l);
    }

    @Override
    public void render(Graphics2D gd) {
        playfield.render(gd);
        gd.setColor(Color.BLACK);
        gd.drawString("" + players.getSize(), 15, 15);
    }


    public static void main(String[] args) {
        // TODO code application logic here
        GameLoader gl = new GameLoader();
        gl.setup(new BGround(), new Dimension(640, 480), false);
        gl.start();
    }

    private void initNet() {

    }
}
